<?php

namespace app\backend\controller;


class ZhTopCard extends Admin
{
    public function index()
    {
        if (!$this->request->isAjax()) {
            return view('index');
        } else {
            $limit           = $this->request->post('limit', 20, 'intval');
            $offset          = $this->request->post('offset', 0, 'intval');
            $where['name']   = $this->request->param('name_s', '', 'serach_in');
            $where['status'] = $this->request->param('status', '', 'serach_in');

            $page = floor($offset / $limit) + 1;
            try {
                $res = \app\backend\model\ZhTopCard::where(formatWhere($where))->order(['sort' => 'desc'])->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
            } catch (\Exception $e) {
                abort(config('my.error_log_code'), $e->getMessage());
            }
            return json(['rows' => $res['data'], 'total' => $res['total']]);
        }
    }

    function selectList()
    {
        return view('select_list');
    }

    /*修改*/
    function update()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            if (!$id) $this->error('参数错误');
            $this->view->assign('info', checkData(\app\backend\model\ZhTopCard::find($id)));
            return view('update');
        } else {
            $postField = 'name,day,price,sort,status,id';
            $data      = $this->request->only(explode(',', $postField), 'post', null);

            $res = \app\backend\model\ZhTopCard::update($data);
            return json(['status' => '00', 'msg' => '修改成功']);
        }
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @author  lwx
     */
    function add()
    {
        if (!$this->request->isPost()) {
            return view('add');
        } else {
            $postField          = 'name,day,price,sort,status';
            $data               = $this->request->only(explode(',', $postField), 'post', null);
            $data['createtime'] = time();
            if (empty($data['price'])) {
                $this->error('请输入售价');
            }

            if (empty($data['day'])) {
                $this->error('请输入天数');
            }

            if (empty($data['name'])) {
                $this->error('请输入名称');
            }

            if (empty($data['sort'])) {
                $this->error('请输入权重');
            }


            $res = \app\backend\model\ZhTopCard::create($data);
            return json(['status' => '00', 'msg' => '添加成功']);
        }
    }

    /*删除*/
    function delete()
    {
        $idx = $this->request->post('id', '', 'serach_in');
        if (!$idx) $this->error('参数错误');
        try {
            \app\backend\model\ZhTopCard::destroy(['id' => explode(',', $idx)], true);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }
}